﻿#include "Menu.h"

Menu::Menu()
{
    this->_hmenu = CreatePopupMenu();
    this->addButton(this->MENU_EXIT, this->LPCWSTR_EXIT);
    this->addSeparator();
}

Menu::~Menu()
{
    DestroyMenu(this->_hmenu);
}

void Menu::dealMenuMessage(HWND handle, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (wParam)
    {
    case this->MENU_EXIT:
        PostQuitMessage(this->MENU_EXIT);
        break;
    }
}

void Menu::addButton(UINT uIDNewItem, LPCWSTR lpNewItem)
{
    MENUITEMINFOW item = {};
    item.cbSize = sizeof(MENUITEMINFO);
    item.fMask = MIIM_ID | MIIM_STRING;
    item.dwTypeData = const_cast<LPWSTR>(lpNewItem); // string值
    item.wID = uIDNewItem; // 菜单标识符
    InsertMenuItemW(this->_hmenu, 0, true, &item);
    // 动态修改需要调用DrawMenuBar(HWND hwnd)重绘菜单
}

void Menu::addSeparator()
{
    MENUITEMINFOW item = {};
    item.cbSize = sizeof(MENUITEMINFO);
    item.fMask = MIIM_FTYPE;
    item.fType = MFT_SEPARATOR; // 水平分隔符
    InsertMenuItemW(this->_hmenu, 0, true, &item);
}

HMENU Menu::getMenu()
{
    return this->_hmenu;
}
